package com.itheima.controller;

import com.itheima.common.PageResult5;
import com.itheima.common.Result3;
import com.itheima.entity.Articles;
import com.itheima.entity.Companys;
import com.itheima.service.ArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/articles")
public class ArticlesController {
    @Autowired
    private ArticlesService articlesService;


    @Autowired
    private HttpServletRequest request;

    //  文章列表  查询
    @GetMapping()
    public PageResult5 findPage(Integer page, Integer pagesize, String keyword, Integer state) {
        PageResult5 pageResult = articlesService.findPage(page, pagesize, keyword, state);
        return pageResult;
    }

    //  文章添加
    @PostMapping
    public Result3 add(@RequestBody Articles articles) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        articlesService.add(articles, username);
        return new Result3(articles.getId());
    }

    //  修改
    @PutMapping("/{id}")
    public Result3 updateById(@PathVariable Integer id, @RequestBody Articles articles) {
        articlesService.updateById(articles);
        return new Result3(true);
    }

    // 删除
    @DeleteMapping("/{id}")
    public Result3 deleteById(@PathVariable Integer id) {
        articlesService.removeById(id);
        return new Result3(true);
    }

    //  修改状态
    @PostMapping("/{id}/{state}")
    public Result3 updateState(@PathVariable Integer id, @PathVariable Integer state) {
        Result3 result3 = articlesService.updateState(id, state);
        return result3;
    }

}
